#ifndef _MUNIX_COMMON_H_
#define _MUNIX_COMMON_H_
/*******************************************************************************
 ** Name: common.hpp                                                          **
 ** Description:                         **
 **                                                                           **
 ** Open Source Initiative (OSI) Approved License                             **
 **                                                                           **
 ** The contents of this file are subject to the terms of the                 **
 ** Common Development and Distribution License, Version 1.0 only             **
 ** (the "License").  You may not use this file except in compliance          **
 ** with the License.                                                         **
 **                                                                           **
 ** You can find a copy of the license in the LICENSE file within             **
 ** this distribution or at $WIKI/display/GOVT/License-software.              **
 ** Software distributed under the License is distributed on an "AS IS"       **
 ** basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.           **
 ** See the License for the specific language governing permissions           **
 ** and limitations under the License.                                        **
 **                                                                           **
 ** When distributing Covered Code, include this CDDL header in each          **
 ** file and include the License file at $GAME_DIR/LICENSE.                   **
 ** If applicable, add the following below this header, with the indicated    **
 ** fields enclosed by brackets "[]" replaced with your own identifying       **
 ** information: Portions Copyright [yyyy] [name of copyright owner]          **
 **                                                                           **
 ** Copyright (c) 2009-2011  Barry Gian James <gian@gamingods.net>            **
 ** All rights reserved.                                                      **
 **                                                                           **
 ** Ref: $HeadURL: https://govtsanct.svn.codeplex.com/svn/trunk/lib/common.hpp $
 ******************************************************************************/
// Modified $Date: 2012-08-14 20:47:30 -0600 (Tue, 14 Aug 2012) $ by $Author: unknown $
// Portions copyright 2012 Open Design Strategies, LLC.
// All rights reserved.
#define _MUNIX_COMMON_ID    "$Id: common.hpp 69068 2012-08-15 02:47:30Z unknown $"

//#include <iostream>
#include <stdint.h>
#include <cwchar>
#include <string>

#include "log.hpp"

// Typedefs for shorthand
// I wonder how much time programmers around the world have lost from having
// to type "_t"...lolz
typedef uint8_t		uint8;
typedef int8_t      int8;
typedef uint16_t	uint16;
typedef int16_t		int16;
typedef uint32_t	uint32;
typedef int32_t		int32;
typedef unsigned char uchar;
typedef wchar_t     wchar;
typedef uint8_t		flags8;
typedef uint16_t	flags16;
typedef uint32_t	flags32;
typedef unsigned int	entity_t;

const std::string WHITESPACE = " \t\n\r";

//class Log;
//extern Log * log;

#define FNAME()			__PRETTY_FUNCTION__
#define CSTR(s)       	(#s)
#define DBGFUNC()       debug::log->out(LOG_DEBUG,"%s",__PRETTY_FUNCTION__)
#define WSTREQ(s1, s2)   ((std::wcscmp (s1, s2) == 0))
#define WNULL			L'\0'
#define MOTD			"../data/txt/MOTD"
#define WZERO(b,sz)		(wmemset(b,0,sz))
#define ZERO(b,sz)		(memset(b,0,sz))

// This just makes my life easier
//#define BIT16(b) ((unsigned long)0x00000001 << (b))
#define BIT32(b) ((uint32)0x00000001 << (b))
#define BIT16(b) ((uint16)0x0001 << (b))
#define	BIT8(b) (0x01 << (b))

// from AVR035: Efficient C Coding for AVR
// Bit MACROs
#define BSET(ADDRESS,BIT) (ADDRESS |= (unsigned char)(1<<BIT))
#define BCLR(ADDRESS,BIT) (ADDRESS &= (unsigned char)~(1<<BIT))
#define BTOG(ADDRESS,BIT) (ADDRESS ^= (unsigned char)(1<<BIT))
#define BCHK(ADDRESS,BIT) (ADDRESS &  (unsigned char)(1<<BIT))

// Bit Mask MACROs
#define BMSET(x,y) (x |= (y))
#define BMCLR(x,y) (x &= (~y))
#define BMTOG(x,y) (x ^= (y))
#define BMCHK(x,y) (x & (y))

#ifndef Max
#define Max(x,y) ((x)>=(y)?(x):(y))
#endif

#ifndef Min
#define Min(x,y) ((x)<=(y)?(x):(y))
#endif

#ifndef Abs
#define Abs(x) ((x) < 0 ? -(x) : (x))
#endif

#ifndef PI
#define PI 3.1415927
#endif

// This creates a CN::pointer member and a CN::create() method
#define SMART_PTR_DECL(CN)				\
	typedef boost::shared_ptr<CN> pointer; 	\
											\
	static pointer Create() {				\
		return pointer(new CN());			\
	}

#define FLYWEIGHT_DECL(CN) \
	typedef boost::flyweights<CN> flyweight; \
											\
	static flyweight CreateFlyweight() {	\
		return flyweight(new CN());			\
	}

#define METERCONV(METERS)	(METERS * 3.281)
#define FEETCONV(FEET)		(FEET / 3.281)
#define FAHRCONV(FAHRENHEIT)    ((FAHRENHEIT-32) * (5/9))
#define CELSIUSCONV(CELSIUS)    ((CELSIUS*1.8)+32)

#if defined(USE_MEM_MGMT)
// These should be moved into the memory management section when it gets written
void * operator new(size_t size);		// new replacement
void * operator new(size_t, void*);		// placement new
void * operator new[] (size_t size);
void operator delete(void * ptr);
void operator delete(void * p, void * const b);	// placement delete..hmmm
void operator delete[] (void * ptr);
#endif

#ifndef __ATTR_CONST__
#define __ATTR_CONST__ __attribute__((__const__))
#endif

#ifndef __ATTR_PURE__
#define __ATTR_PURE__ __attribute__((__pure__))
#endif

//! Takes a hex byte and returns a 2 character string representation
char    Hex2Char(char *);
//! Takes a 2 char string and converts it to an integer hex value
char *  Char2Hex(char);
int     Meters2Feet(int m);
int     Feet2Meters(int f);

std::string Uppercase(const std::string &);
std::string Lowercase(const std::string &);
std::string TrimWhiteSpace(const std::string &);
//template< class type >
//inline std::string toString(const type & t)
//{
//	std::stringstream str;
//	str << t;
//	return str.str();
//}
//template< class type >
//inline type fromString(const std::string & s)
//{
//	std::stringstream str;
//	str << s;
//	type t;
//	str >> t;
//	return t;
//}

struct Dice
{
    int   rolls:3;
    int   num:5;

    static int rnd;
    static int incr() { if (rnd >= 32) rnd = 0; return rnd++; }
    unsigned int   Roll();
};

enum class Locale {
    Alpine, Coniferous, Deciduous, Coastal, Glacier, Scrub, Jungle,
    Mountain, Marine, Grassland, Riparian, Underground, Volcanic,
    Detritus, Desert, Urban, Bog
};
enum class light_type_t { Dark, Shady, Average, Bright, Blinding };

#define WEIGHT_UNLIFTABLE   0





#endif	/* _MUNIX_COMMON_H_ */
